package com.sg.java.test;


import com.sg.java.entity.CMS_VOLT_CURVE;
import com.sg.java.entity.TimeShare;
import com.sg.java.entity.VoltU1_96;
import org.apache.commons.lang3.StringUtils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName TimeSharing
 * @Author wangshuo
 * @Date 2023/7/6 09:22
 * @Version 1.0
 **/
public class TimeSharing222 {
    private static ConcurrentHashMap<String, String> yhMap = new ConcurrentHashMap<>();

    private static HashMap<String, List<CMS_VOLT_CURVE>> pbMap = new HashMap<>();

    //public static void getResult(CMS_VOLT_CURVE curve, String ds) throws Exception {
    //
    //    String pbgx = "pbgx111";
    //    String yhgx = "yhgx111";
    //    String yhdy = "202304yhdycount";
    //
    //    String[] headers = new String[]{"user_id", "volt_code", "meter_id", "org_no", "org_name", "user_name"};
    //    String month = pbgx.substring(4, 6);
    //    String path = ReadHdfsFile.pbFilePath;
    //
    //    yhMap = CsvUtils.CsvToObject(path, headers, month, yhgx, null);
    //    String s = yhMap.get(curve.getMETER_ID());
    //    Gx gx = EntityUtils.StringToYhGx(s);
    //    if (pbMap.get(gx.getPid()) == null) {
    //        List<CMS_VOLT_CURVE> cmsVoltCurves = new ArrayList<>();
    //        cmsVoltCurves.add(curve);
    //        pbMap.put(gx.getPid(), cmsVoltCurves);
    //    } else {
    //        List<CMS_VOLT_CURVE> cmsVoltCurves = pbMap.get(gx.getPid());
    //        cmsVoltCurves.add(curve);
    //    }
    //
    //
    //}

    public static void main(String[] args) throws IntrospectionException, InvocationTargetException, IllegalAccessException, ParseException {


        //变电站1
        //用户1
        ArrayList<CMS_VOLT_CURVE> cmsVoltCurves1 = new ArrayList<>();
        List<VoltU1_96> voltU1961 = new ArrayList<>();
        voltU1961.add(new VoltU1_96(1, "170"));
        voltU1961.add(new VoltU1_96(2, "200"));
        CMS_VOLT_CURVE cmsVoltCurve1 = new CMS_VOLT_CURVE();
        cmsVoltCurve1.setCOL_TIME$U(voltU1961);
        cmsVoltCurve1.setDATA_DATE(new Date());

        //变电站1
        //用户2
        List<VoltU1_96> voltU196122 = new ArrayList<>();
        voltU196122.add(new VoltU1_96(1, "170"));
        voltU196122.add(new VoltU1_96(2, "200"));
        CMS_VOLT_CURVE cmsVoltCurve12 = new CMS_VOLT_CURVE();
        cmsVoltCurve12.setCOL_TIME$U(voltU196122);
        cmsVoltCurve12.setDATA_DATE(new Date());

        cmsVoltCurves1.add(cmsVoltCurve1);
        cmsVoltCurves1.add(cmsVoltCurve12);
        pbMap.put("变电站111111", cmsVoltCurves1);


        //变电站2
        //用户1
        List<VoltU1_96> voltU19612 = new ArrayList<>();
        voltU19612.add(new VoltU1_96(1, "170"));
        voltU19612.add(new VoltU1_96(2, "200"));
        CMS_VOLT_CURVE cmsVoltCurve2 = new CMS_VOLT_CURVE();
        cmsVoltCurve2.setCOL_TIME$U(voltU19612);
        cmsVoltCurve2.setDATA_DATE(new Date());

        ArrayList<CMS_VOLT_CURVE> cmsVoltCurves2 = new ArrayList<>();
        cmsVoltCurves2.add(cmsVoltCurve2);
        pbMap.put("变电站22222222", cmsVoltCurves2);


        System.out.println(pbMap.toString());
        inTimeSharing(pbMap);


        //调用setStateArea方法，并塞入区间状态stateArea

        //PropertyDescriptor stateAreaPD = new PropertyDescriptor("stateArea", TimeShare.class);
        //
        //Method writeMethod = stateAreaPD.getWriteMethod();
        //System.out.println("111111111");
        //writeMethod.invoke(1);

    }

    /**
     * 处理所有数据
     *
     * @param pbMap 所有数据
     * @return 返回处理数据
     */
    public static List<TimeShare> inTimeSharing(HashMap<String, List<CMS_VOLT_CURVE>> pbMap) {


        //用于返回处理好所有数据
        ArrayList<TimeShare> timeShares = new ArrayList<>();

        //遍历pbMap 并处理统计
        pbMap.forEach((k, v) -> {
            //创建15个timeShare 对象，表示15个状态即15条数据
            List<TimeShare> timeShareList = new ArrayList<>();
            for (int i = 1; i <= 15; i++) {
                TimeShare timeShare = new TimeShare();
                timeShareList.add(timeShare);
            }

            for (CMS_VOLT_CURVE cmsVoltCurve : v) {
                List<VoltU1_96> colTime$U = cmsVoltCurve.getCOL_TIME$U();
                for (VoltU1_96 voltU196 : colTime$U) {
                    if (!StringUtils.isEmpty(voltU196.getV())) {
                        double v1 = Double.parseDouble(voltU196.getV());

                        //获取对应字段名 u1...
                        String clazzFieldName = "u" + voltU196.getU();

                        int[] thresholds = {180, 190, 198, 200, 205, 210, 220, 230, 235, 240, 245, 250, 253, 258};
                        int timeShareIndex = 15;

                        int low = 0;
                        int high = thresholds.length - 1;
                        while (low <= high) {
                            int mid = (low + high) / 2;
                            if (v1 < thresholds[mid]) {
                                timeShareIndex = mid + 1;
                                high = mid - 1;
                            } else {
                                low = mid + 1;
                            }
                        }

                        getCount(clazzFieldName, timeShareList.get(timeShareIndex - 1), timeShareIndex);


                        //判断并调用统计方法
//                        if (v1 < 180) {
//                            getCount(clazzFieldName, timeShareList.get(0), 1);
//                        } else if (180 <= v1 && v1 < 190) {
//                            getCount(clazzFieldName, timeShareList.get(1), 2);
//                        } else if (190 <= v1 && v1 < 198) {
//                            getCount(clazzFieldName, timeShareList.get(2), 3);
//                        } else if (198 <= v1 && v1 < 200) {
//                            getCount(clazzFieldName, timeShareList.get(3), 4);
//                        } else if (200 <= v1 && v1 < 205) {
//                            getCount(clazzFieldName, timeShareList.get(4), 5);
//                        } else if (205 <= v1 && v1 < 210) {
//                            getCount(clazzFieldName, timeShareList.get(5), 6);
//                        } else if (210 <= v1 && v1 < 220) {
//                            getCount(clazzFieldName, timeShareList.get(6), 7);
//                        } else if (220 <= v1 && v1 < 230) {
//                            getCount(clazzFieldName, timeShareList.get(7), 8);
//                        } else if (230 <= v1 && v1 < 235) {
//                            getCount(clazzFieldName, timeShareList.get(8), 9);
//                        } else if (235 <= v1 && v1 < 240) {
//                            getCount(clazzFieldName, timeShareList.get(9), 10);
//                        } else if (240 <= v1 && v1 < 245) {
//                            getCount(clazzFieldName, timeShareList.get(10), 11);
//                        } else if (245 <= v1 && v1 < 250) {
//                            getCount(clazzFieldName, timeShareList.get(11), 12);
//                        } else if (250 <= v1 && v1 < 253) {
//                            getCount(clazzFieldName, timeShareList.get(12), 13);
//                        } else if (253 <= v1 && v1 < 258) {
//                            getCount(clazzFieldName, timeShareList.get(13), 14);
//                        } else if (258 < v1) {
//                            getCount(clazzFieldName, timeShareList.get(14), 15);
//                        }
                    }
                }
            }
            //遍历处理后配变的15个状态对象，设置相关属性并塞入到集合返回出去
            for (TimeShare c : timeShareList) {
                c.setStatisticTime(v.get(0).getDATA_DATE());
//                c.setMeter_id(k);
                timeShares.add(c);
            }
            System.out.println(timeShares.size());
            System.out.println(timeShares);
        });
        System.out.println(timeShares);
        return timeShares;
    }


    //1.先构建一个配变的树结构（map）k是配变id v是一个list<CMS_VOLT_CURVE>
    //public void pbTree() throws Exception {
    //
    //    String pbgx = "pbgx111";
    //    String yhgx = "yhgx111";
    //    String yhdy = "202304yhdycount";
    //
    //    String[] headers = new String[]{"user_id", "volt_code", "meter_id", "org_no", "org_name", "user_name"};
    //    String month = pbgx.substring(4, 6);
    //    String path = ReadHdfsFile.pbFilePath;
    //
    //    yhMap = CsvUtils.CsvToObject(path, headers, month, yhgx, null);
    //
    //
    //}

    /**
     * 处理统计方法
     *
     * @param clazzFieldName 对象属性名
     * @param timeShare      15个状态对应的对象
     * @param stateArea      状态区间
     */
    public static void getCount(String clazzFieldName, TimeShare timeShare, int stateArea) {
        try {
            // 获取 timeShare1 的对应字段的 getter 和 setter 方法
            PropertyDescriptor timeSharePD = new PropertyDescriptor(clazzFieldName, TimeShare.class);
            Method timeShareGetter = timeSharePD.getReadMethod();
            Method timeShareSetter = timeSharePD.getWriteMethod();

            // 调用 timeShare 的 getter 方法，获取当前点数对应状态的统计数值
            int result = (int) timeShareGetter.invoke(timeShare);
            //System.out.println("Getter Result: " + result);
            // 调用 timeShare 的 setter 方法，累加数值
            timeShareSetter.invoke(timeShare, result + 1);

            //System.out.println("Setter Result: " + timeShareGetter.invoke(timeShare));

            //调用setStateArea方法，并塞入区间状态stateArea
            PropertyDescriptor stateAreaPD = new PropertyDescriptor("stateArea", TimeShare.class);

            Method writeMethod = stateAreaPD.getWriteMethod();
            //System.out.println("111111111");
            writeMethod.invoke(timeShare, stateArea);
            //System.out.println("111111111");
            //System.out.println("------" + timeShare.toString());


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    //2.遍历map


}
